package constant.milk.periodapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import constant.milk.periodapp.database.DbAdapter;
import constant.milk.periodapp.setting.PasswordActivity;
import constant.milk.periodapp.setting.SengriActivity;
import constant.milk.periodapp.util.MilkShared;

public class IntroActivity extends Activity
{
	private MilkShared s;
	private DbAdapter db;
	
	private Handler mHandler = new Handler() 
	{
		public void handleMessage(Message msg) 
		{
			db = DbAdapter.getInstance();
			db.createTable(IntroActivity.this);
			
			s = new MilkShared(IntroActivity.this);
			
			int flag = s.getFirst();
			
			if (flag == 0)
			{
				startFirstActivity();
			}
			else
			{
				String password = s.getPassword();
				
				if (password.equals(""))
				{
					startMainActivity();
				}
				else
				{
					startPasswordActivity();
				}
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.intro_activity);
		
		mHandler.sendEmptyMessageDelayed(0, 500);
	}
	
	private void startFirstActivity()
	{
		Intent intent = new Intent(IntroActivity.this, SengriActivity.class);
		intent.putExtra(SengriActivity.SENGRI_FLAG, SengriActivity.FIRST);
		startActivity(intent);
		finish();
	}; 
	
	private void startPasswordActivity()
	{
		Intent intent = new Intent(IntroActivity.this, PasswordActivity.class);
		intent.putExtra(PasswordActivity.PASSWORD_FLAG, PasswordActivity.FIRST);
		startActivity(intent);
		finish();
	}; 
	
	private void startMainActivity()
	{
		SengriCheck sc = new SengriCheck(this, db);
		sc.setData();
		
		Intent intent = new Intent(IntroActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}; 
}
